<?php

declare(strict_types=1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use \think\facade\Db;
use think\facade\Cache;

class genProject extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('app\command\genProject')
            ->setDescription('the app\command\genProject command');
    }

    protected function execute(Input $input, Output $output)
    {
        $isin = $this->isin();
        // var_dump($isin);
        // exit;
        $GLOBALS['sql'] = "insert into admin_project (uniq_code,institutions_code,district_code,project_name,budget_size,project_code) values ";
        $rows = Db::table('admin_budget')->where('project_category_code', "2")->group('institutions_code,project_code')->select();
        // echo  Db::table('admin_budget')->getLastSql(). "\n";
        // echo count($rows). "\n";;

        foreach ($rows as $row) {

            $uniq_code = $row['district_code'] . $row['institutions_code'];
            if (in_array($uniq_code, $isin)) {
                echo $uniq_code . " already...\n";
            } else {


                //            $key = 'genProject_' . $uniq_code;
                //            $is = Cache::get($key);
                //            if ($is === NULL) {
                $GLOBALS['sql'] .= "(";
                //                echo $uniq_code . "\n";
                $GLOBALS['sql'] .= "'" . $uniq_code . "','" . $row['institutions_code'] . "','" . $row['district_code'] . "','" . trim($row['project_name']) . "','" . $row['general_public_budget'] . "','" . trim($row['project_code']) . "'),";
                //                Cache::store('redis')->set($key, '1', 7200);
                //            }
                //                echo  substr($GLOBALS['sql'], 0, -1);exit;
            }
        }
        $s2 = substr($GLOBALS['sql'], 0, -1);
        // $s2 = ;
        //        echo $s2 . "\n";
        Db::query($s2);

        exit;
        // 指令输出
        //         $output->writeln('app\command\genbudget');
    }

    public function isin()
    {
        $lists = Db::query("select uniq_code from admin_project");
        $arr = [];
        foreach ($lists as $row) {
            array_push($arr, $row['uniq_code']);
        }
        return $arr;
    }
}
